import {getDic} from "@/api/system/dict.ts";

/**
 * 生成字典
 * @param dicts string || array
 * @returns {Object}
 */
export const createDict = async (dictList:any) => {
    //如果是dicts:String类型，改成Array类型
    if (typeof dictList === "string") dictList = dictList.split('')
    let result:any = {
        dictIndex:[], //索引
        dictList:[] //字典内容
    }
    for (let i in dictList){
        // @ts-ignore
        result.dictIndex.push(dictList[i])
        // @ts-ignore
        result.dictList.push(await getDic(dictList[i]))
    }
    return result
}

/**
 * 字典值转HTML标签
 * @param dictKey 要转换的字典名称
 * @param value 字典值
 * @param dictIndex 字典值索引
 * @param dictList 字典内容
 */
export const dictKeyToValue = (dictKey:string, value, dictIndex:Array<String>=[], dictList=[]) => {
    // @ts-ignore
    const index = dictIndex.indexOf(dictKey)
    const dict:any = dictList[index]
    for (let i in dict){
        // @ts-ignore
        if (dict[i].dictValue == value){
            // @ts-ignore
            return `<span class="dict-tag ${dict[i].listClass}">${dict[i].dictLabel}</span>`
        }
    }
    // return `<span class="dict-tag danger">暂无</span>`
    return `<span class="dict-tag danger">暂无</span>`
}


export const dictKeyToSelect = (dictKey, dictIndex=[], dictList=[]) => {
    // @ts-ignore
    const index = dictIndex.indexOf(dictKey)
    const dict = dictList[index]
    return dict
}